package com.java.androidmarket.utils;

import com.java.androidmarket.entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * description:
 * author:张腾
 * date:2021-04-23
 */
@Component
public class RedisUtils {

    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public RedisUtils getRedisUtils(){
        return new RedisUtils();
    }
    /**
     * 写入缓存
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key,Object value){
        boolean result = false;
        try{
            redisTemplate.opsForValue().set(key,value);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 携带过期时间写入缓存
     * @param key
     * @param value
     * @param expireTime
     * @param timeUnit
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime , TimeUnit timeUnit) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            redisTemplate.expire(key, expireTime, timeUnit);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断缓存是否存在
     * @param key
     * @return
     */
    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     * @param key
     * @return
     */
    public Object get(final String key) {
        Object result = null;
        result = redisTemplate.opsForValue().get(key);
        return result;
    }

    /**
     * 哈希 写入缓存
     * @param key
     * @param hashKey
     * @param value
     */
    public void hmSet(String key, Object hashKey, Object value){
        redisTemplate.opsForHash().put(key,hashKey,value);
    }

    /**
     * 哈希 读取缓存
     * @param key
     * @param hashKey
     * @return
     */
    public Object hmGet(String key, Object hashKey){
        return redisTemplate.opsForHash().get(key,hashKey);
    }

    /**
     * 取出多个goods
     * @param key
     * @param list
     * @return
     */
    public List<Goods> hmMutiGet(String key, Set<String> list){
        return redisTemplate.opsForHash().multiGet(key,list);
    }

    /**
     * 存入多个goods
     * @param key
     * @param map
     */
    public void hmMutiSet(String key, Map<String,Object> map){
        redisTemplate.opsForHash().putAll(key,map);
    }

    /**
     * 获取key中hmKey的长度
     * @param key
     * @return
     */
    public int hmKeysNum(String key){
        Set keys = redisTemplate.opsForHash().keys(key);
        return keys.size();
    }

    /**
     * 获取key中的所有hmKey
     * @param key
     * @return
     */
    public Set<String> hmGetKeys(String key){
        return redisTemplate.opsForHash().keys(key);
    }

    /**
     * 哈希 判断缓存是否存在
     * @param key
     * @param hmKey
     * @return
     */
    public boolean hmExist(String key,Object hmKey){
        return redisTemplate.opsForHash().hasKey(key,hmKey);
    }

    /**
     * 删除指定key的值
     * @param key
     * @return
     */
    public Boolean del(String key){
        return redisTemplate.delete(key);
    }

    public void zSet(String key,Object value,double score){
        redisTemplate.opsForZSet().add(key,value,score);
    }

    public Set zGet(String key){
        return redisTemplate.opsForZSet().reverseRange(key, 0, -1);
    }

    public void zDelOne(String key){
        redisTemplate.opsForZSet().removeRange(key,0,0);
    }
}
